//
//  MyDynamicView.m
//  Comma
//
//  Created by dlios on 15-1-13.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MyDynamicView.h"
#import "Masonry.h"
#import "MyDynamicTableViewCell.h"
#import "MyCustomView.h"
#import "GlobalMacro.h"
#import "GlobalMethod.h"
#import "MyAnotherTableViewCell.h"
#import "AFNetWorkingYU.h"
#import "URLMacro.h"
#import "MyDynamicModel.h"
#import "UIImageView+WebCache.h"

@implementation MyDynamicView

#pragma mark - 创建一个tableview

#pragma mark - 所有绿的字体 都是兰亭黑-中  图片还需要调整

#pragma mark - 滚动条有一个回弹效果

- (instancetype)initWithLeftButton:(NSString *)leftTitle RightButton:(NSString *)rightTitle Title:(NSString *)title FooterView:(FooterViewState)footer
{
    self = [super initWithLeftButton:leftTitle RightButton:rightTitle Title:title FooterView:footer];
    if (self) {
        self.tableArray = [NSMutableArray array];
        self.tableOther = [NSMutableArray array];
        self.anotherArray = [NSMutableArray array];
    }
    return self;
}
#pragma mark - 重写tablearray的set方法  放tablearray被赋值的时候会直接走这个方法
- (void)setTableArray:(NSMutableArray *)tableArray
{
    _tableArray = tableArray;
    [self.tableView reloadData];
}
- (void)setTableOther:(NSMutableArray *)tableOther
{
    _tableOther = tableOther;
    [self.tableView reloadData];
}
- (void)setAnotherArray:(NSMutableArray *)anotherArray
{
    _anotherArray = anotherArray;
    [self.tableView reloadData];
}
- (void)createMyDynamic
{
    [self setBackgroundColor:[UIColor whiteColor]];
    self.clickLeft = YES;
    self.clickRight = NO;
    [self createMyDynamicTable];

}
//重写父类的左侧的button
- (void)creatLeftButtonWithTitle:(NSString *)leftTitle
{
    //图片大小 21 13
    self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.leftButton setFrame:CGRectMake(0, 20, 48, 44)];
    [self.leftButton setBackgroundImage:[UIImage imageNamed:@"Button_drawer(1)"] forState:UIControlStateNormal];
    
    [self.leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:self.leftButton];
    
    
    
    
    NSLog(@"已经重新创建左侧按钮！！！！");
}

- (void)createMyDynamicTable
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.tableView registerClass:[MyDynamicTableViewCell class] forCellReuseIdentifier:@"cell1"];
    [self.tableView registerClass:[MyAnotherTableViewCell class] forCellReuseIdentifier:@"cell2"];
    [self addSubview:self.tableView];
    [_tableView setBackgroundColor:[UIColor clearColor]];
    [self.tableView setShowsVerticalScrollIndicator:NO];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(@(64));
        make.width.mas_equalTo(@(SCREEN_WIDTH));
        make.height.mas_equalTo(@(SCREEN_HEIGHT - 113));
    }];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.clickLeft == YES) {
        return [self.tableArray count];
    }
    else
    {
        return [self.anotherArray count];
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.clickLeft == YES && self.clickRight == NO) {
        MyDynamicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        MyDynamicModel *model = [self.tableArray objectAtIndex:indexPath.row];
        NSString *point = @" •";
        NSString *category = [NSString stringWithFormat:@"%@%@", model.category, point];
        [cell.myTitle setText:model.title];
        [cell.myCategory setText:category];
        [cell.myBadge setText:model.myUnreadMsg];
        [cell.myPhoto sd_setImageWithURL:[NSURL URLWithString:model.puberImg] placeholderImage:nil options:SDWebImageRetryFailed];
        [cell.myTime setText:model.pubTime];
        
        if ([cell.myBadge.text isEqualToString:@"0"]) {
            cell.myBadge.hidden = YES;
        }
        else
        {
            cell.myBadge.hidden = NO;
        }

        NSLog(@"%@", cell.myBadge.text);
        return cell;
    }
    else
    {
        MyAnotherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        MyDynamicModel *model = [self.anotherArray objectAtIndex:indexPath.row];
        NSString *point = @" • ";
        NSString *category = [NSString stringWithFormat:@"%@%@",model.category, point];
        [cell.myTitle setText:model.title];
        [cell.myCategory setText:category];
        [cell.myBadge setText:model.myUnreadMsg];
        [cell.myPhoto sd_setImageWithURL:[NSURL URLWithString:model.puberImg] placeholderImage:nil options:SDWebImageRetryFailed];
        [cell.myTime setText:model.pubTime];
        
        NSString *puberInfo = [GlobalMethod puberNickChangeString:model.puberRole];
        [cell.infoLabel setText:puberInfo];
        NSString *school = [NSString stringWithFormat:@" • %@",model.puberSchool];
        [cell.puberSchool setText:school];
    
        [cell.userName setText:model.puberNick];

        
        
        
        
        
        if ([cell.myBadge.text isEqualToString:@"0"]) {
            cell.myBadge.hidden = YES;
        }
        else
        {
            cell.myBadge.hidden = NO;
        }

        

        return cell;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.clickLeft == YES) {
        MyDynamicModel *model = [self.tableArray objectAtIndex:indexPath.row];
        
        [self.delegate transferModel:model otherDic:[self.tableOther objectAtIndex:indexPath.row]];
    }
    else
    {
        MyDynamicModel *model = [self.anotherArray objectAtIndex:indexPath.row];
        [self.delegate transferModel:model otherDic:nil];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    //添加两个button 分别是我发布的 和我参与的
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    [myView setBackgroundColor:[UIColor whiteColor]];
    MyCustomView *myHeaderView = [[MyCustomView alloc] initWithFrame:CGRectMake(0, 2, SCREEN_WIDTH, 40)];
    [myView addSubview:myHeaderView];
    [myHeaderView setBackgroundColor:[UIColor whiteColor]];
//    if (self.clickLeft == YES && self.clickRight == NO) {
//        [myHeaderView.leftButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
//        [myHeaderView.rightButton setTitleColor:TEXTERGRAY forState:UIControlStateNormal];
//    }
//    else
//    {
//        [myHeaderView.leftButton setTitleColor:TEXTERGRAY forState:UIControlStateNormal];
//        [myHeaderView.rightButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
//    }
    
    
    [GlobalMethod drawLineWithStartPoint:CGPointMake(0, 44) EndPoint:CGPointMake(SCREEN_WIDTH, 44) inUIView:myView];
    
//    [myHeaderView.leftButton addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
//    [myHeaderView.rightButton addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
//    myHeaderView.leftButton.tag = 10;
//    myHeaderView.rightButton.tag = 11;
    
    //button下面的滑动条
    
  //每次点击button都会走这个方法  所以 如果存在 的话 就不许要再重建scrollView了。
//    if (!self.scrollView) {
//        [self createCustomScroll];
//    }
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake((self.frame.size.width - 60)/2, 42, 60, 2)];
    [view setBackgroundColor:TEXTGREENCOLOR];
    [myView addSubview:view];
    return myView;
    
}
- (UIView *)createCustomScroll
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(115, 106, 60, 2)];
    [_scrollView setBackgroundColor:TEXTGREENCOLOR];
    
    [self addSubview:_scrollView];
    return _scrollView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}
//- (void)leftAction:(id)sender
//{
//    
//    self.clickLeft = YES;
//    self.clickRight = NO;
//    [self scrollToLeft];
//    UIButton *leftButton = (UIButton *)sender;
//    [leftButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
//    UIButton *rightButton = (UIButton *)[self.tableView viewWithTag:11];
//    [rightButton setTitleColor:TEXTERGRAY forState:UIControlStateNormal];
//    [self.tableView reloadData];
//    self.tableView.contentOffset = CGPointMake(0, 0);
//}
//- (void)rightAction:(id)sender
//{
//    self.clickRight = YES;
//    self.clickLeft = NO;
//    [self scrollToRight];
//    UIButton *rightButton = (UIButton *)sender;
//    [rightButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
//    UIButton *leftButton = (UIButton *)[self.tableView viewWithTag:10];
//    [leftButton setTitleColor:TEXTERGRAY forState:UIControlStateNormal];
//    [self.tableView reloadData];
//    self.tableView.contentOffset = CGPointMake(0, 0);
////    [self.tableView reloadData];
//}
//
//#pragma mark - 滑动条移动
//- (void)scrollToLeft
//{
//    
//    
//    self.scrollView.frame = CGRectMake(210, 106, 60, 2);
//
//    [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
//        [self.scrollView setFrame:CGRectMake(105, 106, 60, 2)];
//    } completion:^(BOOL finished) {
//        [UIView animateWithDuration:0.08 animations:^{
//            [self.scrollView setFrame:CGRectMake(115, 106, 60, 2)];
//
//        }];
//      
//    }];
//    
//    
//}
//
//- (void)scrollToRight
//{
//
//    [self.scrollView setFrame:CGRectMake(115, 106, 60, 2)];
//    [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
//        [self.scrollView setFrame:CGRectMake(210, 106, 60, 2)];
//
//    } completion:^(BOOL finished) {
//        [UIView animateWithDuration:0.05 animations:^{
//            [self.scrollView setFrame:CGRectMake(200, 106, 60, 2)];
//            
//        }];
//    }];
//
//}
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    NSLog(@"拉动y =  %f, x = %f ", scrollView.contentOffset.y,scrollView.contentOffset.x);
//    if (scrollView.contentOffset.y < 0) {
//        [self.scrollView setFrame:CGRectMake(self.scrollView.frame.origin.x,  106 - scrollView.contentOffset.y, 60, 2)];
//    }
//    else
//    {
//        [self.scrollView setFrame:CGRectMake(self.scrollView.frame.origin.x, 106, 60, 2)];
//    }
//}
//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
//{
////    NSLog(@"拉动y =  %f, x = %f ", scrollView.contentOffset.y,scrollView.contentOffset.x);
//}
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//    [self.scrollView setFrame:CGRectMake(self.scrollView.frame.origin.x, 106, 60, 2)];
//}

@end
